<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\CustomerService;
use App\Repos\OrderRepo;

class CustomerServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
        $param = [];
        $query = CustomerService::query();

        if($request->keyword) {
            $code = trim($request->keyword);
            $query->whereHas('order',function($query) use  ($code) {
                $query->where('code','like','%'.$code.'%');
            });
            $param['keyword'] = $code;
        }
        $list  = $query->paginate(10);
        return view('admin.customer-service.index',compact('list','param'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {   

        $data = CustomerService::find($id);
        $orderStatus = OrderRepo::getStatus();
        //dd($orderStatus);
        return view('admin.customer-service.edit',compact('data','orderStatus'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rs['status'] = true;
        $rs['msg']    = '操作成功';
        $data = $request->except(['_token','_method']);
        $data['id'] = $id;
        $flag = OrderRepo::customerComplete($data);  
        if($flag['status']==false) {
            $rs['status'] = false;
            $rs['msg'] = $flag['msg'];
            return back()->with('rs',$rs);
        }  
        return redirect('zadmin/customer-service')->with('rs',$rs);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        CustomerService::destroy($id);
        return back();
    }
}
